//
//  XTUtility+File.m
//  XTUtilityKit
//
//  Created by Tong on 2017/5/19.
//  Copyright © 2017年 Tong. All rights reserved.
//

#import "XTUtility+File.h"

@implementation XTUtility (File)

#pragma mark - 获取沙盒目录相关路径

/** 获取沙盒主目录路径 */
+ (NSString *)getHomeDirectory
{
    return NSHomeDirectory();
}

/** 获取沙盒中的 Documents 目录路径 */
+ (NSString *)getDocumentsDirectory
{
    return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
}

/** 获取沙盒中的 Liabrary 目录路径 */
+ (NSString *)getLibraryDirectory
{
    return [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) lastObject];
}

/** 获取沙盒中的 Caches 目录路径 */
+ (NSString *)getCachesDirectory
{
    return [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
}

/** 获取沙盒中 Temporary 目录路径  */
+ (NSString *)getTemporaryDirectory
{
    return NSTemporaryDirectory();
}

/** 获取文件目录路径 */
+ (NSString *)getFileDirectory:(NSString *)path
{
    return [path stringByDeletingLastPathComponent];
}

/** 获取文件名字 */
+ (NSString *)getFileNameWithFilePath:(NSString *)path needSuffix:(BOOL)needSuffix
{
    NSString *fileName = [path lastPathComponent];
    if (!needSuffix) {
        fileName = [fileName stringByDeletingPathExtension];
    }
    return fileName;
}

/** 获取文件类型 */
+ (NSString *)getFileSuffixWithFilePath:(NSString *)path
{
    return [path pathExtension];
}

/** 获取文件属性 */
+ (NSDictionary *)getFileAttributesOfItemAtPath:(NSString *)path
{
    return [self getFileAttributesOfItemAtPath:path error:nil];
}

/** 获取文件属性,返回错误信息 */
+ (NSDictionary *)getFileAttributesOfItemAtPath:(NSString *)path error:(NSError **)error
{
    return [[NSFileManager defaultManager] attributesOfItemAtPath:path error:error];
}

/** 获取文件某一属性 */
+ (id)getOneFileAttributesOfItemAtPath:(NSString *)path byFileAttributeType:(NSFileAttributeType)fileAttributeType
{
    return [self getOneFileAttributesOfItemAtPath:path byFileAttributeType:fileAttributeType error:nil];
}

/** 获取文件某一属性,返回错误信息 */
+ (id)getOneFileAttributesOfItemAtPath:(NSString *)path byFileAttributeType:(NSFileAttributeType)fileAttributeType error:(NSError **)error
{
    NSDictionary *attributeDic = [self getFileAttributesOfItemAtPath:path error:error];
    id attribute = [attributeDic objectForKey:fileAttributeType];
    return attribute;
}

#pragma mark - 检查文件相关信息

/** 判断文件路径是否存在 */
+ (BOOL)checkFileIsExistsAtPath:(NSString *)path
{
    return [[NSFileManager defaultManager] fileExistsAtPath:path];
}

/** 判断文件是否可以执行 */
+ (BOOL)checkFileIsExecutableItemAtPath:(NSString *)path
{
    return [[NSFileManager defaultManager] isExecutableFileAtPath:path];
}

/** 判断文件是否可读 */
+ (BOOL)checkFileIsReadableItemAtPath:(NSString *)path
{
    return [[NSFileManager defaultManager] isReadableFileAtPath:path];
}

/** 判断文件是否可写 */
+ (BOOL)checkFileIsWritableItemAtPath:(NSString *)path
{
    return [[NSFileManager defaultManager] isWritableFileAtPath:path];
}

#pragma mark - 创建文件夹

/** 创建目录 */
+ (BOOL)createDirectoryAtPath:(NSString *)path
{
    return [self createDirectoryAtPath:path error:nil];
}

/** 创建目录 */
+ (BOOL)createDirectoryAtPath:(NSString *)path error:(NSError **)error
{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    BOOL success = [fileManager createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:error];
    return success;
}

/** 创建文件 */
+ (BOOL)createFileAtPath:(NSString *)path
{
    return [self createFileAtPath:path error:nil];
}

/** 创建文件 */
+ (BOOL)createFileAtPath:(NSString *)path error:(NSError **)error
{
    // 判断文件夹路径存不存在，不存在则先创建文件夹
    NSString *directoryPath = [self getFileDirectory:path];
    if (![self checkFileIsExistsAtPath:directoryPath]) {
        if (![self createDirectoryAtPath:directoryPath error:error]) {
            return NO;
        }
    }
    BOOL success = [[NSFileManager defaultManager] createFileAtPath:path contents:nil attributes:nil];
    return success;
}

#pragma mark - 删除文件(夹)

/** 删除文件(夹) */
+ (BOOL)removeFileAtPath:(NSString *)path
{
    return [self removeFileAtPath:path];
}

/** 删除文件(夹) */
+ (BOOL)removeFileAtPath:(NSString *)path error:(NSError **)error
{
    return [[NSFileManager defaultManager] removeItemAtPath:path error:error];
}

#pragma mark - 复制文件(夹)

/** 复制文件(夹) */
+ (BOOL)copyFileAtPath:(NSString *)path toPath:(NSString *)toPath
{
    return [self copyFileAtPath:path toPath:toPath error:nil];
}

/** 复制文件(夹), 返回错误信息 */
+ (BOOL)copyFileAtPath:(NSString *)path toPath:(NSString *)toPath error:(NSError **)error
{
    return [self copyOrMoveFileAtPath:path toPath:toPath isCopy:YES error:error];;
}

#pragma mark - 移动文件(夹)

/** 移动文件(夹) */
+ (BOOL)moveFileAtPath:(NSString *)path toPath:(NSString *)toPath
{
    return [self moveFileAtPath:path toPath:toPath error:nil];
}

/** 移动文件(夹),返回错误信息 */
+ (BOOL)moveFileAtPath:(NSString *)path toPath:(NSString *)toPath error:(NSError **)error
{
    return [self copyOrMoveFileAtPath:path toPath:toPath isCopy:NO error:error];;
}

/** 复制文件(夹) Or 移动文件(夹), 返回错误信息 */
+ (BOOL)copyOrMoveFileAtPath:(NSString *)path toPath:(NSString *)toPath isCopy:(BOOL)isCopy error:(NSError **)error
{
    // 检查源文件是否存在,不存在抛出异常
    if (![self checkFileIsExistsAtPath:path]) {
        [NSException raise:@"源文件非法路径" format:@"源文件路径%@不存在,请检查源文件路径", path];
        return NO;
    }
    
    // 检查目标目录路径是否存在,不存在则创建
    NSString *toDirPath = [self getFileDirectory:toPath];
    if (![self checkFileIsExistsAtPath:toDirPath]) {
        if (![self createDirectoryAtPath:toDirPath error:error]) {
            return NO;
        }
    }
    
    // 检查目标是否存在,存在则删除
    if ([self checkFileIsExistsAtPath:toPath]) {
        [self removeFileAtPath:toPath error:error];
    }
    
    BOOL success = NO;
    if (isCopy) {
        success = [[NSFileManager defaultManager] copyItemAtPath:path toPath:toPath error:error];
        return success;
    }
    
    success = [[NSFileManager defaultManager] moveItemAtPath:path toPath:toPath error:error];
    return success;
}

@end
